﻿using Microsoft.EntityFrameworkCore;
using ProcessManagementApi.AllError;
using ProcessManagementApi.Services;
using MESData.Infrastucture;
using MESData.Infrastucture.Interface;
using MESData.Infrastucture.Impement;

namespace ProcessManagementApi.Extensions
{
	/// <summary>
	/// 扩展方法 静态类 静态方法 this关键字
	/// </summary>
	public static class ServiceCollectionExtensions
	{
		public static WebApplicationBuilder Inject(this WebApplicationBuilder builder)
		{
			// Add services to the container.
			//注册全局异常处理
			builder.Services.AddControllers(x => x.Filters.Add<GloabIExcptionfilter>());

			// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
			builder.Services.AddEndpointsApiExplorer();
			//中文注释
			builder.Services.AddSwaggerGen(x => x.IncludeXmlComments(AppContext.BaseDirectory + "ProcessManagementApi.xml", true));
			//注册上下文 开发环境下启用SQL日志记录
			builder.Services.AddDbContext<MyDbcontext>(x =>
			{
				x.UseMySql(builder.Configuration.GetConnectionString("db"), new MySqlServerVersion("5.7"), options =>
				{
					// 配置连接池和并发查询选项
					options.EnableRetryOnFailure(maxRetryCount: 3, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
				});
				// 启用服务提供程序缓存
				x.EnableServiceProviderCaching();
				// 启用敏感数据日志记录（仅在开发环境）
				if (builder.Environment.IsDevelopment())
				{
					x.EnableSensitiveDataLogging();
					x.LogTo(Console.WriteLine, LogLevel.Information);
				}
			});

			//配置Base模块
			builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

			//注册工序分类服务
			builder.Services.AddScoped<IProcessCategoryService, ProcessCategoryService>();

			//注册工序服务
			builder.Services.AddScoped<ProcessManagementApi.Services.IProcessService, ProcessService>();
			
			//注册工艺流程服务
			builder.Services.AddScoped<IProcessFlowService, ProcessFlowService>();

			return builder;
		}
	}
}
